package thread.io;

import model.User;
import org.junit.Test;
import thread.test1Io.PipedInThread;
import thread.test1Io.PipedOutThread;

import java.io.*;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Date;
import java.util.Enumeration;
import java.util.Vector;

public class TestInputStream {

	/**
	 *
	 * 标记后从新读取
	 */
	@Test
	public void test1() throws IOException {
		InputStream in = new BufferedInputStream(new FileInputStream(new File(
				"d:/test.txt")));
		if (!in.markSupported()) {
			System.out.println("mark/rset is not supported");
			return;
		}

		int ch;
		int count = 0;
		boolean marked = false;
		while ((ch = in.read()) != -1) {
			System.out.println("." + ch);

			if ((ch == 4) && !marked) {
				// ??4??λ????
				in.mark(10);
				marked = true;
			}
			if (ch == 8 && count < 2) {
				// ????λ???4
				in.reset();
				count++;
			}
		}

		in.close();
	}

	/**
	 *
	 *字节数组流
	 *
	 * */
	@Test
	public void ByteArrayOutputStreamTest() throws IOException {
		FileInputStream fis = new FileInputStream(new File(
				"E:/document/pdf/aaa.pdf"));
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		int len = 0;
		byte[] bt = new byte[1024];
		while ((len = fis.read(bt)) != -1) {
			bos.write(bt);
		}
		System.out.println();
		ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
		byte[] bt2 = new byte[1024];
		while ((bis.read(bt2) != -1)) {
			System.out.println(Arrays.toString(bt2));
		}
		bis.close();
		fis.close();
		bos.close();
		System.out.println();

	}

	/**
	 *
	 * @throws IOException
	 * */
	@Test
	public void test3() throws IOException {
		String str = "American navy";
		InputStream in = new StringBufferInputStream(str);
		byte[] bt = new byte[1];
		int len = 0;
		while ((len = in.read(bt)) != -1) {
			System.out.println((char) bt[0]);
		}
		in.close();
	}

	/**
	 *
	 * 用于java 序列化 和反序列化
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	@Test
	public void test4() throws IOException, ClassNotFoundException {
		User user1 = new User("zhangsan", 20, new Date(), "xuliehua");
		User user2 = new User("lisi", 30, new Date(), "xuliehua");
		FileOutputStream fos = new FileOutputStream("d:/a.txt");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(user1);
		oos.writeObject(user2);
		oos.close();
		FileInputStream fis = new FileInputStream(new File("d:/a.txt"));
		ObjectInputStream ois = new ObjectInputStream(fis);
		User user = (User) ois.readObject();
		System.out.println(user.getName());
		ois.close();
	}

	/** ################################################################# */


	/**
	 * 每次读取一个字节
	 * @throws IOException
     */

	@Test
	public void test5() throws IOException {
		FileInputStream fis = new FileInputStream(new File("d:/a.txt"));
		int len = 0;
		while ((len = fis.read()) != -1) {
			System.out.println((char)len);
		}
		fis.close();
	}

	/**
	 * 每次读取一个数组，末尾被0填充
	 * @throws IOException
     */
	@Test
	public void test6() throws IOException {
		FileInputStream fis = new FileInputStream(new File("d:/a.txt"));
		int len = 0;
		byte[] bt = new byte[1024];
		while ((len = fis.read(bt)) != -1) {
			System.out.println(Arrays.toString(bt));
		}
		fis.close();

	}

	/**
	 * 文件输入输出
	 * @throws IOException
     */
	@Test
	public void test7() throws IOException {
		File file = new File("d:/a.txt");
		FileInputStream fis = new FileInputStream(file);
		int len = 0;
		byte[] bt = new byte[(int) file.length()];
		fis.read(bt);
		System.out.println(Arrays.toString(bt));
		fis.close();
	}

	/**
	 * 缓冲字节流
	 * @throws IOException
     */
	@Test
	public void test8() throws IOException {
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
				new File("d:/a.txt")));
		byte[] bt = new byte[1024];
		int len = 0;
		while ((len = bis.read(bt)) != -1) {
			System.out.println(Arrays.toString(bt));
		}
		bis.close();

	}

	/** ################################################################ */
	/**
	 *
	 * @throws IOException
	 * */
	@Test
	public void test9() throws IOException {
		Vector<FileInputStream> vector = new Vector<>();
		vector.add(new FileInputStream(new File("d:/a.txt")));
		vector.add(new FileInputStream(new File("d:/b.txt")));
		Enumeration<FileInputStream> en = vector.elements();
		SequenceInputStream sq = new SequenceInputStream(en);
		BufferedInputStream bis = new BufferedInputStream(sq);
		int len = 0;
		byte[] bt = new byte[1024];
		while ((len = bis.read(bt)) != -1) {
			System.out.println(Arrays.toString(bt));
		}

	}

	/**
	 * 管道字节流，一般用于多线程
	 * PipedInputStream
	 *
	 * @throws IOException
	 * */
	@Test
	public void test10() throws IOException {
		PipedOutputStream pos = new PipedOutputStream();
		PipedInputStream pis = new PipedInputStream();
		pos.connect(pis);
		new PipedOutThread(pos).start();
		new PipedInThread(pis).start();
	}

	/** ############################################################# */

	/**
	 *
	 * @throws IOException
	 */
	@Test
	public void test11() throws IOException {
		ByteArrayInputStream bin = new ByteArrayInputStream(
				"hello world".getBytes());
		BufferedInputStream bis = new BufferedInputStream(bin);
		int len = 0;
		byte[] bt = new byte[1024];
		while ((len = bis.read(bt)) != -1) {
			System.out.println(Arrays.toString(bt));
		}
	}

	/** ############################################ */
	/**
	 * digestOutputStream
	 *
	 * @description
	 * @version currentVersion
	 * @author mj
	 * @throws NoSuchAlgorithmException
	 */
	@Test
	public void test42() throws NoSuchAlgorithmException {
		File file=new File("d:/test.txt");
		try {
			FileInputStream fin=new FileInputStream(file);
			MessageDigest sha=MessageDigest.getInstance("sha-256");
			DigestInputStream din=new DigestInputStream(fin, sha);
			//System.out.println(DatatypeConverter.printHexBinary(sha.digest()));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
	}

}
